package com.base.po.acess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.base.utils.Produce;

import java.util.Date;

/**
 * 获取token的工具类
 *
 * @author Administrator
 *
 */
public class TokenUtils {
	private final static String Key = "zjandzyc";

	/**
	 * 获取Token
	 */
	public static String getToken(String parentid, String mobile) {
		if (parentid == null || parentid.equals("") || mobile == null || mobile.equals("")) {
			return null;
		}
		AcessToken acessToken = new AcessToken(parentid, mobile);
		return DesUtil.encrypt(JSON.toJSONString(acessToken), DesUtil.CHARSET_GB2312, Key);
	}


	public static AcessToken getAcessToken(String tokenCode) {
		if (tokenCode == null) {
			return null;
		}
		try {
			String tokenCodeJson = DesUtil.decrypt(tokenCode, DesUtil.CHARSET_GB2312, Key);
			AcessToken token = JSONObject.parseObject(tokenCodeJson, AcessToken.class);
			Date tokenDate = new Date(token.getCurrenttime());
			Date pushDate = Produce.plusDay(1, tokenDate);
			if (Produce.getNowDate().after(pushDate)) {
				token = null;
			}
			return token;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
